/**  
 * All rights Reserved, Designed By Android_Robot   
 * @Title:  ZoneAllQunZuActivity.java   
 * @Package com.jiyi.activity.zone   
 * @Description:    TODO(用一句话描述该文件做什么)   
 * @author: HYM     
 * @date:   2017-6-12 下午2:48:18   
 * @version V1.0     
 */
package com.jiyi.activity.zone;

import android.content.Intent;
import android.graphics.Color;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v4.widget.SwipeRefreshLayout.OnRefreshListener;
import android.view.View;
import android.widget.ExpandableListView;
import android.widget.ExpandableListView.OnChildClickListener;
import android.widget.TextView;

import com.android.volley.VolleyError;
import com.google.gson.Gson;
import com.jiyi.adapter.HuoDongAdapter;
import com.jiyi.bean.zone.HuoDongDetailJson;
import com.jiyi.bean.zone.ZoneAllHuoDongJson;
import com.jiyi.bean.zone.ZoneHuoDongBean;
import com.jiyi.bean.zone.ZoneHuoDongParent;
import com.jiyi.common.StringInterface;
import com.jiyi.common.VolleyRequest;
import com.jiyi.event.HuoDongListEvent;
import com.jiyi.inteface.HuoDongPopInterface;
import com.jiyi.inteface.WebApi;
import com.jiyi.view.HuoDongPopView;
import com.swift.base.ActivityBase;
import com.swift.maintenance.R;
import com.swift.maintenance.base.AppConfig;
import com.swift.maintenance.utils.ToastUtil;
import com.swift.maintenance.utils.UserInfoUtil;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * @ClassName: ZoneAllQunZuActivity
 * @Description:TODO(这里用一句话描述这个类的作用)
 * @author: HYM
 * @date: 2017-6-12 下午2:48:18
 * 
 */
public class ZoneAllHuoDongActivity extends ActivityBase implements
		HuoDongPopInterface, OnRefreshListener {

	@BindView(R.id.tvBack)
	TextView tvBack;
	@BindView(R.id.tvTitle)
	TextView tvTitle;
	@BindView(R.id.tvSearch)
	TextView tvSearch;

	@BindView(R.id.refreshLayout)
	SwipeRefreshLayout refreshLayout;
	@BindView(R.id.srlTip)
	SwipeRefreshLayout srlTip;

	@BindView(R.id.mainList)
	ExpandableListView mainList;

	HuoDongAdapter mHuoDongAdapter;

	ArrayList<ZoneHuoDongParent> mHuoDongDatas = new ArrayList<ZoneHuoDongParent>();

	HuoDongPopView mHuoDongPopView;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_all_huodong);
		ButterKnife.bind(this);
		init();
	}
	
	@Subscribe
	public void onHuoDongListEvent(HuoDongListEvent huoDongListEvent)
	{
		int type = huoDongListEvent.type;
		switch (type) {
		case HuoDongListEvent.ALL_HUODONG_FRAGMENT:
			getHuoDongData();
			break;

		default:
			break;
		}
	}
	
	@Override
	protected void onDestroy() {
		EventBus.getDefault().unregister(this);
		super.onDestroy();
	}

	private void init() {
		setTop();
		setAdapter();
		getHuoDongData();
		mHuoDongPopView = new HuoDongPopView(this, this);
		EventBus.getDefault().register(this);
	}

	/**
	 * @author: HYM
	 * @Title: setAdapter
	 * @Description: TODO(这里用一句话描述这个方法的作用) void
	 * @date: 2017-7-25
	 */
	private void setAdapter() {
		// 设置refreshLayout的监听
		refreshLayout.setColorSchemeColors(Color.RED, Color.YELLOW, Color.BLUE,
				Color.GREEN);
		refreshLayout.setOnRefreshListener(this);
		srlTip.setColorSchemeColors(Color.RED, Color.YELLOW, Color.BLUE,
				Color.GREEN);
		srlTip.setOnRefreshListener(this);
		
		mHuoDongAdapter = new HuoDongAdapter(this, mHuoDongDatas);
		mainList.setAdapter(mHuoDongAdapter);
		mainList.setGroupIndicator(null);// 这个是去掉父级的箭头
		// 点击不能收缩
		mainList.setOnGroupClickListener(new ExpandableListView.OnGroupClickListener() {

			@Override
			public boolean onGroupClick(ExpandableListView parent, View v,
					int groupPosition, long id) {
				return true;
			}
		});
		mainList.setOnChildClickListener(new OnChildClickListener() {

			@Override
			public boolean onChildClick(ExpandableListView parent, View v,
					int groupPosition, int childPosition, long id) {
				ZoneHuoDongBean zoneHuoDongBean = mHuoDongDatas
						.get(groupPosition).getChildList().get(childPosition);
				getHuoDongDetail(zoneHuoDongBean.getId());
				return true;
			}
		});
	}

	/**
	 * @author: HYM
	 * @Title: getHuoDongData
	 * @Description: TODO(这里用一句话描述这个方法的作用) void
	 * @date: 2017-6-30
	 */
	private void getHuoDongData() {
		Map<String, String> params = new HashMap<String, String>();
		params.put(AppConfig.ACCESS_TOKEN, UserInfoUtil.getCurrentUserAccessToken());

		VolleyRequest.StringRequestPost(this, WebApi.ZONE_ALL_HUODONG_LIST, "",
				params, new StringInterface(this) {

					@Override
					public void stringSuccess(String result) {
						try {
							JSONObject obj = new JSONObject(result);
							String status = obj.getString("status");
							String info = obj.getString("info");
							if (status.equals("200")) {
								ZoneAllHuoDongJson temp = new Gson().fromJson(
										result, ZoneAllHuoDongJson.class);
								if (temp != null
										&& temp.getData() != null
										&& temp.getData()
												.getActivityRecommend().size() == 0
										&& temp.getData().getActivityList()
												.size() == 0) {
									srlTip.setVisibility(View.VISIBLE);
									refreshLayout.setVisibility(View.GONE);
								} else {
									srlTip.setVisibility(View.GONE);
									refreshLayout.setVisibility(View.VISIBLE);
									mHuoDongDatas.clear();
									ZoneHuoDongParent tjhd = new ZoneHuoDongParent();
									tjhd.setTitle("推荐活动");
									tjhd.setChildList(temp.getData()
											.getActivityRecommend());
									mHuoDongDatas.add(tjhd);
									ZoneHuoDongParent zxhd = new ZoneHuoDongParent();
									zxhd.setTitle("最新活动");
									zxhd.setChildList(temp.getData()
											.getActivityList());
									mHuoDongDatas.add(zxhd);
									mHuoDongAdapter.notifyDataSetChanged();
									for (int i = 0; i < mHuoDongDatas.size(); i++) {
										mainList.expandGroup(i);
									}
								}

							} else {
								ToastUtil.getInstances().showMsg(info);
							}
						} catch (JSONException e) {
							e.printStackTrace();
						} finally {
							refreshLayout.setRefreshing(false);
						}
					}

					@Override
					public void stringError(VolleyError error) {
						refreshLayout.setRefreshing(false);
						ToastUtil.getInstances().showMsg(R.string.err_http_response);
					}
				});
	}

	/**
	 * @author: HYM
	 * @Title: setTop
	 * @Description: TODO(这里用一句话描述这个方法的作用) void
	 * @date: 2017-6-12
	 */
	private void setTop() {
		tvBack.setText("");
		tvTitle.setText("全部活动");
		Drawable searchDraw = getResources()
				.getDrawable(R.drawable.icon_search);
		searchDraw.setBounds(0, 0, searchDraw.getMinimumWidth(),
				searchDraw.getMinimumHeight());
		tvSearch.setCompoundDrawables(searchDraw, null, null, null);
		tvSearch.setText("");
	}

	@OnClick({ R.id.tvBack, R.id.tvSearch })
	public void onClick(View veiw) {
		Intent intent = null;
		switch (veiw.getId()) {
		case R.id.tvBack:
			finish();
			break;
		case R.id.tvSearch:
			intent = new Intent(this, ZoneSearchActivity.class);
			intent.putExtra("type", 1);
			startActivity(intent);
			break;
		default:
			break;
		}
	}

	/**
	 * <p>
	 * Title: OnSureListener
	 * </p>
	 * <p>
	 * Description:
	 * </p>
	 * 
	 * @see HuoDongPopInterface#OnSureListener()
	 */
	@Override
	public void OnSureListener() {
		// TODO Auto-generated method stub

	}

	/**
	 * <p>
	 * Title: OnCancleListener
	 * </p>
	 * <p>
	 * Description:
	 * </p>
	 * 
	 * @see HuoDongPopInterface#OnCancleListener()
	 */
	@Override
	public void OnCancleListener() {
		// TODO Auto-generated method stub

	}

	/**
	 * <p>
	 * Title: OnShowListener
	 * </p>
	 * <p>
	 * Description:
	 * </p>
	 * 
	 * @see HuoDongPopInterface#OnShowListener()
	 */
	@Override
	public void OnShowListener() {
		// TODO Auto-generated method stub

	}

	/**
	 * <p>
	 * Title: OnHideListener
	 * </p>
	 * <p>
	 * Description:
	 * </p>
	 * 
	 * @see HuoDongPopInterface#OnHideListener()
	 */
	@Override
	public void OnHideListener() {
		// TODO Auto-generated method stub

	}

	/**
	 * @author: HYM
	 * @Title: getHuoDongDetail
	 * @Description: TODO(获取活动详情)
	 * @param huoDongId
	 *            void
	 * @date: 2017-7-17
	 */
	void getHuoDongDetail(String huoDongId) {

		Map<String, String> params = new HashMap<String, String>();
		params.put(AppConfig.ACCESS_TOKEN, UserInfoUtil.getCurrentUserAccessToken());
		params.put("id", huoDongId);

		VolleyRequest.StringRequestPost(this, WebApi.ZONE_HUODONG_DETAIL, "",
				params, new StringInterface(this) {

					@Override
					public void stringSuccess(String result) {
						try {
							JSONObject obj = new JSONObject(result);
							String status = obj.getString("status");
							String info = obj.getString("info");
							if (status.equals("200")) {
								HuoDongDetailJson temp = new Gson().fromJson(
										result, HuoDongDetailJson.class);
								mHuoDongPopView.showPopWindow(temp.getData());
							} else {
								ToastUtil.getInstances().showMsg(info);
							}
						} catch (JSONException e) {
							e.printStackTrace();
						} finally {
						}
					}

					@Override
					public void stringError(VolleyError error) {
						ToastUtil.getInstances().showMsg(R.string.err_http_response);
					}
				});
	}

	@Override
	public void onRefresh() {
		getHuoDongData();
	}
}
